

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.feature.min_max_scaler &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.feature.min_max_scaler</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.feature.min_max_scaler</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">Iterable</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.statics</span> <span class="k">import</span> <span class="n">MultivariateStatisticalSummary</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.data_overview</span> <span class="k">import</span> <span class="n">get_header</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic</span> <span class="k">import</span> <span class="n">data_overview</span>


<div class="viewcode-block" id="MinMaxScaler"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.min_max_scaler.MinMaxScaler">[docs]</a><span class="k">class</span> <span class="nc">MinMaxScaler</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Transforms features by scaling each feature to a given range,e.g.between minimum and maximum. The transformation is given by:</span>
<span class="sd">            X_scale = (X - X.min) / (X.max - X.min), while X.min is the minimum value of feature, and X.max is the maximum</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;normal&#39;</span><span class="p">,</span> <span class="n">area</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">,</span> <span class="n">feat_upper</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">feat_lower</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">out_upper</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">out_lower</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        mode: str, the mode just support &quot;normal&quot; now, and will support &quot;cap&quot; mode in the future.</span>
<span class="sd">              for mode is &quot;normal&quot;, the feat_upper and feat_lower is the normal value and for &quot;cap&quot;, feat_upper and</span>
<span class="sd">              feature_lower will between 0 and 1, which means the percentile of the column. Default &quot;normal&quot;</span>

<span class="sd">        area: str. It supports &quot;all&quot; and &quot;col&quot;. For &quot;all&quot;,feat_upper/feat_lower will act on all data column,</span>
<span class="sd">            so it will just be a value, and for &quot;col&quot;, it just acts on one column they corresponding to,</span>
<span class="sd">            so feat_lower/feat_upper will be a list, which size will equal to the number of columns</span>

<span class="sd">        feat_upper: int or float, the upper limit in the column. If the value is larger than feat_upper, it will be set to feat_upper. Default None.</span>
<span class="sd">        feat_lower: int or float, the lower limit in the column. If the value is less than feat_lower, it will be set to feat_lower. Default None.</span>
<span class="sd">        out_upper: int or float,  the results of scale will be mapped to the area between out_lower and out_upper.Default None.</span>
<span class="sd">        out_upper: int or float, the results of scale will be mapped to the area between out_lower and out_upper.Default None.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mode</span> <span class="o">=</span> <span class="n">mode</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">area</span> <span class="o">=</span> <span class="n">area</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span> <span class="o">=</span> <span class="n">feat_upper</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span> <span class="o">=</span> <span class="n">feat_lower</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out_upper</span> <span class="o">=</span> <span class="n">out_upper</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out_lower</span> <span class="o">=</span> <span class="n">out_lower</span>

    <span class="k">def</span> <span class="nf">__get_min_max_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get each column minimum and maximum</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">min_value</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">max_value</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">summary_obj</span> <span class="o">=</span> <span class="n">MultivariateStatisticalSummary</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">header</span> <span class="o">=</span> <span class="n">get_header</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">max_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">min_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span>

        <span class="k">if</span> <span class="n">min_value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">max_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">min_value_dict</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_min</span><span class="p">()</span>
            <span class="n">min_value_list</span> <span class="o">=</span> <span class="p">[</span> <span class="n">min_value_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">header</span> <span class="p">]</span>

            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">max_value</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">max_value</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">min_value_list</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;Size of feat_upper is not equal to column of data, </span><span class="si">{}</span><span class="s2"> != </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">max_value</span><span class="p">)),</span>
                                                                                             <span class="nb">len</span><span class="p">(</span><span class="n">min_value_list</span><span class="p">)))</span>
                <span class="n">max_value_list</span> <span class="o">=</span> <span class="n">max_value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">max_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">max_value</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">min_value_list</span><span class="p">]</span>

        <span class="k">elif</span> <span class="n">min_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">max_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">max_value_dict</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_max</span><span class="p">()</span>
            <span class="n">max_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">max_value_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">header</span><span class="p">]</span>

            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">min_value</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">min_value</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">max_value_list</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;Size of feat_lower is not equal to column of data, </span><span class="si">{}</span><span class="s2"> != </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">max_value</span><span class="p">)),</span>
                                                                                             <span class="nb">len</span><span class="p">(</span><span class="n">max_value_list</span><span class="p">)))</span>
                <span class="n">min_value_list</span> <span class="o">=</span> <span class="n">min_value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">min_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">min_value</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">max_value_list</span><span class="p">]</span>

        <span class="k">elif</span> <span class="n">min_value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">max_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">min_value_dict</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_min</span><span class="p">()</span>
            <span class="n">max_value_dict</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_max</span><span class="p">()</span>
            <span class="n">min_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">min_value_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">header</span><span class="p">]</span>
            <span class="n">max_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">max_value_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">header</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">shape</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">max_value</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">):</span>
                <span class="n">max_value_list</span> <span class="o">=</span> <span class="n">max_value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">shape</span> <span class="o">=</span> <span class="n">data_overview</span><span class="o">.</span><span class="n">get_features_shape</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
                <span class="n">max_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">max_value</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">shape</span><span class="p">)]</span>

            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">min_value</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">):</span>
                <span class="n">min_value_list</span> <span class="o">=</span> <span class="n">min_value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">shape</span><span class="p">:</span>
                    <span class="n">shape</span> <span class="o">=</span> <span class="n">data_overview</span><span class="o">.</span><span class="n">get_features_shape</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

                <span class="n">min_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">min_value</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">shape</span><span class="p">)]</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">max_value_list</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">min_value_list</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;Size of feat_upper is not equal to column of data, </span><span class="si">{}</span><span class="s2"> != </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">max_value</span><span class="p">)),</span>
                                                                                         <span class="nb">len</span><span class="p">(</span><span class="n">min_value_list</span><span class="p">)))</span>

        <span class="k">return</span> <span class="n">min_value_list</span><span class="p">,</span> <span class="n">max_value_list</span>

    <span class="k">def</span> <span class="nf">__get_upper_lower_percentile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="c1"># TODO</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">__check_param</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check if input parameter is legal</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">support_mode</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;normal&#39;</span><span class="p">,</span> <span class="s1">&#39;cap&#39;</span><span class="p">]</span>
        <span class="n">support_area</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;col&#39;</span><span class="p">,</span> <span class="s1">&#39;all&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mode</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">support_mode</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupport mode:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mode</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">area</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">support_area</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupport area:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">area</span><span class="p">))</span>

        <span class="n">check_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">check_value</span> <span class="ow">in</span> <span class="n">check_value_list</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">check_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">area</span> <span class="o">==</span> <span class="s1">&#39;all&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                            <span class="s2">&quot;for area is all, </span><span class="si">{}</span><span class="s2"> should be int or float, not </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">check_value</span><span class="p">)))</span>

                <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">area</span> <span class="o">==</span> <span class="s1">&#39;col&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">):</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                            <span class="s2">&quot;for area is col, </span><span class="si">{}</span><span class="s2"> should be Iterable, not </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">check_value</span><span class="p">)))</span>

        <span class="n">check_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">out_upper</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_lower</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">check_value</span> <span class="ow">in</span> <span class="n">check_value_list</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">check_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;for area is all, </span><span class="si">{}</span><span class="s2"> should be int or float, not </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">check_value</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">check_value</span><span class="p">)))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">area</span> <span class="o">==</span> <span class="s1">&#39;all&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;for area is all, feat_upper should not less than feat_lower, but </span><span class="si">{}</span><span class="s2"> &lt; </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span><span class="p">))</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">area</span> <span class="o">==</span> <span class="s1">&#39;col&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">))</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span><span class="p">)):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;for area is col, sizeof feat_upper should equal to the sizeof feat_lower, but </span><span class="si">{}</span><span class="s2"> != </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">)),</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span><span class="p">))))</span>

                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">))):</span>
                    <span class="k">if</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">&lt;</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span><span class="p">[</span><span class="n">i</span><span class="p">]):</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                            <span class="s2">&quot;for area is col, feat_upper[</span><span class="si">{}</span><span class="s2">] should not less than feat_lower[</span><span class="si">{}</span><span class="s2">], but </span><span class="si">{}</span><span class="s2"> &lt; </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">,</span>
                                                                                                                      <span class="n">i</span><span class="p">,</span>
                                                                                                                      <span class="bp">self</span><span class="o">.</span><span class="n">feat_upper</span><span class="p">[</span>
                                                                                                                          <span class="n">i</span><span class="p">],</span>
                                                                                                                      <span class="bp">self</span><span class="o">.</span><span class="n">feat_lower</span><span class="p">[</span>
                                                                                                                          <span class="n">i</span><span class="p">]))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_upper</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_lower</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">out_upper</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">out_lower</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;for area is all, out_upper should not less than out_lower, but </span><span class="si">{}</span><span class="s2"> &lt; </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">out_upper</span><span class="p">,</span>
                                                                                                    <span class="bp">self</span><span class="o">.</span><span class="n">out_lower</span><span class="p">))</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">__scale_with_cols_for_instance</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">max_value_list</span><span class="p">,</span> <span class="n">min_value_list</span><span class="p">,</span> <span class="n">scale_value_list</span><span class="p">,</span> <span class="n">out_lower</span><span class="p">,</span> <span class="n">out_scale</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Scale operator for each column. The input data type is data_instance</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
            <span class="k">if</span> <span class="n">data</span><span class="o">.</span><span class="n">features</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">max_value_list</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="n">data</span><span class="o">.</span><span class="n">features</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">min_value_list</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">features</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">min_value_list</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">/</span> <span class="n">scale_value_list</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

            <span class="n">data</span><span class="o">.</span><span class="n">features</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">value</span> <span class="o">*</span> <span class="n">out_scale</span> <span class="o">+</span> <span class="n">out_lower</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">data</span>

<div class="viewcode-block" id="MinMaxScaler.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.min_max_scaler.MinMaxScaler.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Apply min-max scale for input data</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data: data_instance, input data</span>

<span class="sd">        Returns</span>
<span class="sd">        ----------</span>
<span class="sd">        fit_data:data_instance, data after scale</span>
<span class="sd">        cols_transform_value: list of tuple, each tuple include minimum, maximum, output_minimum, output maximum</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__check_param</span><span class="p">()</span>

        <span class="c1"># if self.mode == &#39;normal&#39;:</span>
        <span class="n">min_value</span><span class="p">,</span> <span class="n">max_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__get_min_max_value</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="n">out_lower</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_lower</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_lower</span>
        <span class="n">out_upper</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_upper</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_upper</span>

        <span class="n">out_scale</span> <span class="o">=</span> <span class="n">out_upper</span> <span class="o">-</span> <span class="n">out_lower</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">out_scale</span> <span class="o">-</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1e-6</span> <span class="ow">or</span> <span class="n">out_scale</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;out_scale should large than 0&quot;</span><span class="p">)</span>

        <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">data_scale</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">max_value</span><span class="p">)):</span>
            <span class="n">scale</span> <span class="o">=</span> <span class="n">max_value</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">min_value</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">scale</span> <span class="o">-</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1e-6</span> <span class="ow">or</span> <span class="n">scale</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;scale value should large than 0&quot;</span><span class="p">)</span>
            <span class="n">data_scale</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">scale</span><span class="p">)</span>
            <span class="n">cols_transform_value</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">min_value</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">max_value</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">out_lower</span><span class="p">,</span> <span class="n">out_upper</span><span class="p">))</span>

        <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">MinMaxScaler</span><span class="o">.</span><span class="n">__scale_with_cols_for_instance</span><span class="p">,</span> <span class="n">max_value_list</span><span class="o">=</span><span class="n">max_value</span><span class="p">,</span>
                              <span class="n">min_value_list</span><span class="o">=</span><span class="n">min_value</span><span class="p">,</span> <span class="n">scale_value_list</span><span class="o">=</span><span class="n">data_scale</span><span class="p">,</span> <span class="n">out_lower</span><span class="o">=</span><span class="n">out_lower</span><span class="p">,</span>
                              <span class="n">out_scale</span><span class="o">=</span><span class="n">out_scale</span><span class="p">)</span>
        <span class="n">fit_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">fit_data</span><span class="p">,</span> <span class="n">cols_transform_value</span></div>

<div class="viewcode-block" id="MinMaxScaler.transform"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.min_max_scaler.MinMaxScaler.transform">[docs]</a>    <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">cols_transform_value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Transform input data using min-max scale with fit results</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data: data_instance, input data</span>
<span class="sd">        cols_transform_value: list of tuple, the return of fit function. Each tuple include minimum, maximum, output_minimum, output maximum</span>
<span class="sd">        Returns</span>
<span class="sd">        ----------</span>
<span class="sd">        transform_data:data_instance, data after transform</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">max_value</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">min_value</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">out_upper</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">out_lower</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">data_scale</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">cols_transform_value</span><span class="p">:</span>
            <span class="n">min_value</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">col</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">max_value</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">col</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">out_lower</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">col</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
            <span class="n">out_upper</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">col</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>

            <span class="n">scale</span> <span class="o">=</span> <span class="n">col</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">col</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">scale</span> <span class="o">-</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1e-6</span> <span class="ow">or</span> <span class="n">scale</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;scale value should large than 0&quot;</span><span class="p">)</span>
            <span class="n">data_scale</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">scale</span><span class="p">)</span>

        <span class="c1"># check if each value of out_upper or out_lower is same</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cols_transform_value</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">cols_transform_value</span><span class="p">)):</span>
                <span class="k">if</span> <span class="n">out_lower</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="n">out_lower</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="ow">or</span> <span class="n">out_upper</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="n">out_upper</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;In transform out_lower or out_upper have not same value&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;cols_transform_value&#39;s size should larger than 0&quot;</span><span class="p">)</span>

        <span class="n">out_scale</span> <span class="o">=</span> <span class="n">out_upper</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">out_lower</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">out_scale</span> <span class="o">-</span> <span class="mi">0</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1e-6</span> <span class="ow">or</span> <span class="n">out_scale</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;out_scale should large than 0&quot;</span><span class="p">)</span>

        <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">MinMaxScaler</span><span class="o">.</span><span class="n">__scale_with_cols_for_instance</span><span class="p">,</span> <span class="n">max_value_list</span><span class="o">=</span><span class="n">max_value</span><span class="p">,</span>
                              <span class="n">min_value_list</span><span class="o">=</span><span class="n">min_value</span><span class="p">,</span> <span class="n">scale_value_list</span><span class="o">=</span><span class="n">data_scale</span><span class="p">,</span> <span class="n">out_lower</span><span class="o">=</span><span class="n">out_lower</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                              <span class="n">out_scale</span><span class="o">=</span><span class="n">out_scale</span><span class="p">)</span>

        <span class="n">fit_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">fit_data</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>